package com.lkm.app_ui.m.Imple.push.captive;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Looper;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lkm.app_ui.AppConst;
import com.mj.hx.yl.push.OnReceiverPushListener;
import com.mj.hx.yl.push.PushHandle;
import com.mj.hx.yl.push.PushHandleList;
import com.mj.hx.yl.push.PushManage;



public class OnAppPushReceiver extends BroadcastReceiver {

    public static void regMe() {
        AppConst.getApplication().registerReceiver(new OnAppPushReceiver(), new IntentFilter("com.lkm.action.push_"+AppConst.getApplication().getPackageName()), "com.lkm.permission.push", new Handler(Looper.getMainLooper()));
    }

    public void onReceive(final Context context, Intent intent) {

        String json = intent.getStringExtra("json");
        receiverContent(context,json);

    }

    public static void receiverContent(Context context, String customContent){
        OnReceiverPushListener or = PushManage.onReceiverPushListener;

        if(or!=null)
            if(or.onReceiverPush_TextMessage(customContent)) return;

        if (customContent != null && customContent.length() != 0) {
            System.out.println("=========push " + customContent);
            customContent = customContent.trim();
            try {
                if (customContent.startsWith("[")) {

                    JsonParser jp  = new JsonParser();
                    JsonArray  jas = (JsonArray) jp.parse(customContent);
                    for (int i = 0; i < jas.size(); i++) {
                        handle(context, (JsonObject) jas.get(i));
                    }

                } else {
                    JsonParser jp = new JsonParser();
                    JsonObject js = (JsonObject) jp.parse(customContent);
                    handle(context, js);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    public static boolean handle(final Context context, JsonObject js) {

        // System.out.println("=========PushReceiverData   " + json);

        if (js == null)
            return false;
        try {
            int         type;
            JsonElement stateJe = js.get("t");
            if (stateJe != null) {
                type = stateJe.getAsInt();
            } else {

                return false;
            }
            // if(result.type<0||result.type>10)


            final PushHandle<Object> ph = PushHandleList.getPushHandle(type);


            if (ph == null)
                return false;

            Object datat = null;
            if (ph.getDataClass() == null) {
                datat = null;
            } else {
                JsonElement dataJe = js.get("d");
                if (dataJe == null)
                    return false;
                datat = new Gson().fromJson(dataJe, ph.getDataClass());
            }
            final Object data = datat;


            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    ph.handler(context, data);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }
}
